1 package org.smartcomps.twister.engine.core;
2
3 import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
4 import net.sf.hibernate.cfg.Configuration;
5
6 import org.smartcomps.twister.common.transaction.TransactionManager;
7 import org.smartcomps.twister.common.lifecycle.LifecycleManager;
8 import org.smartcomps.twister.engine.priv.core.definition.ActivityFactory;
9 import org.smartcomps.twister.engine.priv.core.definition.ProcessFactory;
10 import org.smartcomps.twister.engine.priv.core.definition.Sequence;
11 import org.smartcomps.twister.engine.priv.core.definition.Wait;
12 import org.smartcomps.twister.engine.priv.core.definition.TwisterProcess;
13 import org.smartcomps.twister.engine.priv.core.definition.Activity;
14 import org.smartcomps.twister.engine.priv.core.definition.Empty;
15 import org.smartcomps.twister.engine.priv.core.definition.Receive;
16 import org.smartcomps.twister.engine.priv.core.definition.Reply;
17 import org.smartcomps.twister.engine.priv.core.definition.Invoke;
18 import org.smartcomps.twister.engine.priv.core.definition.Assign;
19 import org.smartcomps.twister.engine.priv.core.definition.Assignment;
20 import org.smartcomps.twister.engine.priv.core.definition.While;
21 import org.smartcomps.twister.engine.priv.core.definition.Scope;
22 import org.smartcomps.twister.engine.priv.core.definition.Terminate;
23 import org.smartcomps.twister.engine.priv.core.definition.Pick;
24 import org.smartcomps.twister.engine.priv.core.definition.Switch;
25 import org.smartcomps.twister.engine.priv.core.definition.CorrelationRef;
26 import org.smartcomps.twister.engine.priv.core.definition.MessageEvent;
27 import org.smartcomps.twister.engine.priv.core.definition.AlarmEvent;
28 import junit.framework.TestCase;
29
30 import java.util.Iterator;
31 import java.util.HashSet;
32
33 /***
34 * Creates a small network of activities and checks everything
35 * around; like the links relationships and the container
36 * relationships.
37 */
38 public class TestActivity extends TestCase {
39
40 protected void setUp() throws Exception {
41 LifecycleManager.getLifecycleManager().createResources();
42 LifecycleManager.getLifecycleManager().startResources();
43
44 SchemaExport schemaExport = new SchemaExport(new Configuration().configure());
45 schemaExport.create(true, true);
46
47 TransactionManager.beginTransaction();
48 }
49
50 protected void tearDown() throws Exception {
51 TransactionManager.commitTransaction();
52
53 LifecycleManager.getLifecycleManager().stopResources();
54 LifecycleManager.getLifecycleManager().destroyResources();
55 }
56
57 public void testCreate() throws Exception {
58 // Creating process
59 TwisterProcess testProcess = ProcessFactory.createProcess("TestProcess");
60
61 // Creating root sequence with its activities
62 Sequence testSequence = (Sequence) ActivityFactory.createActivity(Sequence.class, testProcess);
63 {
64 Receive activity1 = (Receive) ActivityFactory.createActivity(Receive.class, testSequence);
65 activity1.setName("act1");
66
67 Reply activity2 = (Reply) ActivityFactory.createActivity(Reply.class, testSequence);
68 activity2.setName("act2");
69
70 Empty activity3 = (Empty) ActivityFactory.createActivity(Empty.class, testSequence);
71 activity3.setName("act3");
72
73 While testWhile = (While) ActivityFactory.createActivity(While.class, testSequence);
74 testWhile.setCondition("false");
75 {
76 Scope testScope = (Scope) ActivityFactory.createActivity(Scope.class, testWhile);
77 {
78 Wait activity4 = (Wait) ActivityFactory.createActivity(Wait.class, testScope);
79 activity4.setName("act4");
80 }
81 }
82
83 Invoke activity5 = (Invoke) ActivityFactory.createActivity(Invoke.class, testSequence);
84 activity5.setName("act5");
85
86 Assign activity6 = (Assign) ActivityFactory.createActivity(Assign.class, testSequence);
87 activity6.setName("act6");
88 Assignment assignment1 = ActivityFactory.addAssignment(activity6, Assignment.VARIABLE_PART, Assignment.VARIABLE_PART);
89 assignment1.setFromFirstValue("var");
90 assignment1.setFromSecondValue("part");
91 assignment1.setToFirstValue("var2");
92 assignment1.setToSecondValue("part2");
93 Assignment assignment2 = ActivityFactory.addAssignment(activity6, Assignment.VARIABLE_PART, Assignment.VARIABLE_PART);
94 assignment2.setFromFirstValue("var2");
95 assignment2.setFromSecondValue("part2");
96 assignment2.setToFirstValue("var3");
97 assignment2.setToSecondValue("part3");
98
99 Switch testSwitch = (Switch) ActivityFactory.createActivity(Switch.class, testSequence);
100 {
101 Invoke activity7 = (Invoke) ActivityFactory.createActivity(Invoke.class, testSwitch);
102 activity7.setName("act7");
103 testSwitch.addCondition("today == monday", activity7);
104
105 Invoke activity8 = (Invoke) ActivityFactory.createActivity(Invoke.class, testSwitch);
106 activity8.setName("act8");
107 testSwitch.addCondition("today == thursday", activity8);
108
109 Terminate activity9 = (Terminate) ActivityFactory.createActivity(Terminate.class, testSwitch);
110 activity9.setName("act9");
111 testSwitch.setOtherwise(activity9);
112 }
113
114 Pick testPick = (Pick) ActivityFactory.createActivity(Pick.class, testSequence);
115 testPick.setCreateInstance(true);
116 {
117 Reply activity10 = (Reply) ActivityFactory.createActivity(Reply.class, testPick);
118 activity10.setName("act10");
119 Reply activity11 = (Reply) ActivityFactory.createActivity(Reply.class, testPick);
120 activity11.setName("act11");
121 Terminate activity12 = (Terminate) ActivityFactory.createActivity(Terminate.class, testPick);
122 activity12.setName("act12");
123
124 CorrelationRef cRef1 = ActivityFactory.createCorrelationRef("set1", false, CorrelationRef.OUT);
125 CorrelationRef cRef2 = ActivityFactory.createCorrelationRef("set2", false, CorrelationRef.OUT);
126 HashSet correlations1 = new HashSet();
127 correlations1.add(cRef1);
128 correlations1.add(cRef2);
129 MessageEvent mEvent1 = ActivityFactory.createMessageEvent("link1", "portType1", "operation1", "variable1", correlations1);
130 testPick.addMessageEvent(mEvent1, activity10);
131
132 CorrelationRef cRef3 = ActivityFactory.createCorrelationRef("set3", true, CorrelationRef.IN);
133 HashSet correlations2 = new HashSet();
134 correlations2.add(cRef3);
135 MessageEvent mEvent2 = ActivityFactory.createMessageEvent("link2", "portType2", "operation2", "variable2", correlations2);
136 testPick.addMessageEvent(mEvent2, activity11);
137
138 AlarmEvent aEvent = ActivityFactory.createAlarmEvent("2j", AlarmEvent.DURATION_EXPR);
139 testPick.addAlarmEvent(aEvent, activity12);
140 }
141
142 }
143
144 TransactionManager.commitTransaction();
145 TransactionManager.beginTransaction();
146
147 TwisterProcess process = ProcessFactory.getByName("TestProcess");
148 Activity rootActivity = process.getActivity();
149
150 assertTrue("The root activity is not a sequence as expected", rootActivity instanceof Sequence);
151 assertEquals("There are not 8 elements in the sequence", 8, ((Sequence)rootActivity).getActivities().size());
152
153 Iterator activities = ((Sequence)rootActivity).getActivities().iterator();
154
155 Activity first = (Activity) activities.next();
156 assertTrue("The first activity is not a Wait as expected", first instanceof Receive);
157 assertEquals("The first activity name is wrong", "act1", first.getName());
158
159 Activity second = (Activity) activities.next();
160 assertTrue("The second activity is not a Wait as expected", second instanceof Reply);
161 assertEquals("The second activity name is wrong", "act2", second.getName());
162
163 Activity third = (Activity) activities.next();
164 assertTrue("The third activity is not a Wait as expected", third instanceof Empty);
165 assertEquals("The third activity name is wrong", "act3", third.getName());
166
167 Activity aWhile = (Activity) activities.next();
168 assertTrue("The activity is not a While as expected", aWhile instanceof While);
169 assertEquals("The while doesn't have the right condition", "false", ((While)aWhile).getCondition());
170 assertTrue("The activity in the while is not a Wait as expected", ((Scope)((While)aWhile).getActivities().get(0)).getActivities().get(0) instanceof Wait);
171 assertEquals("The wait activity contained in the While name is wrong", "act4", ((Wait)((Scope)((While)aWhile).getActivities().get(0)).getActivities().get(0)).getName());
172
173 Activity fifth = (Activity) activities.next();
174 assertTrue("The fifth activity is not a Wait as expected", fifth instanceof Invoke);
175 assertEquals("The fifth activity name is wrong", "act5", fifth.getName());
176
177 Activity sixth = (Activity) activities.next();
178 assertTrue("The sixth activity is not a Assign as expected", sixth instanceof Assign);
179 assertEquals("The sixth activity name is wrong", "act6", sixth.getName());
180 assertEquals("The sixth activity assign doesn't have 2 assignments", 2, ((Assign)sixth).getAssignments().size());
181
182 Activity aSwitch = (Activity) activities.next();
183 assertTrue("The activity is not a Switch as expected", aSwitch instanceof Switch);
184 assertEquals("The Switch doesn't have 3 elements", 3, ((Switch)aSwitch).getActivityConditions().size());
185 {
186 Iterator switchActIter = ((Switch)aSwitch).getActivityConditions().keySet().iterator();
187
188 Activity seventh = (Activity) switchActIter.next();
189 assertTrue("The seventh activity is not a Invoke as expected", seventh instanceof Invoke);
190 assertEquals("The seventh activity name is wrong", "act7", seventh.getName());
191 assertEquals("The Switch first condition is wrong", "today == monday", ((Switch)aSwitch).getActivityConditions().get(seventh));
192
193 Activity eighth = (Activity) switchActIter.next();
194 assertTrue("The eighth activity is not a Invoke as expected", eighth instanceof Invoke);
195 assertEquals("The eighth activity name is wrong", "act8", eighth.getName());
196 assertEquals("The Switch second condition is wrong", "today == thursday", ((Switch)aSwitch).getActivityConditions().get(eighth));
197
198 Activity nineth = (Activity) switchActIter.next();
199 assertTrue("The nineth activity is not a Terminate as expected", nineth instanceof Terminate);
200 assertEquals("The nineth activity name is wrong", "act9", nineth.getName());
201 }
202
203 Activity aPick = (Pick) activities.next();
204 assertTrue("The activity is not a Pick as expected", aPick instanceof Pick);
205 assertEquals("The Pick doesn't have 3 elements", 3, ((Pick)aPick).getActivities().size());
206 assertEquals("The Pick doesn't have 2 message events", 2, ((Pick)aPick).getActivityMessageEvents().size());
207 assertEquals("The Pick doesn't have 1 alarm event", 1, ((Pick)aPick).getActivityAlarmEvents().size());
208 {
209 Iterator msgEventIter = ((Pick)aPick).getActivityMessageEvents().keySet().iterator();
210 Activity tenth = (Activity) msgEventIter.next();
211 assertTrue("The first activity in Pick is not a Reply as expected", tenth instanceof Reply);
212 assertEquals("The first activity in Pick name is wrong", "act10", tenth.getName());
213 MessageEvent msgEvent1 = ((Pick)aPick).getMessageEvent(tenth);
214 assertEquals("The message event associated with the first activity in Pick has not right link", "link1", msgEvent1.getPartnerLink());
215 {
216 Iterator corrIter1 = msgEvent1.getCorrelations().iterator();
217 CorrelationRef corr1 = (CorrelationRef) corrIter1.next();
218 assertTrue("The first correlation associated with the first activity in Pick has not right set", corr1.getSet().equals("set1") || corr1.getSet().equals("set2"));
219 CorrelationRef corr2 = (CorrelationRef) corrIter1.next();
220 assertTrue("The second correlation associated with the first activity in Pick has not right set", corr2.getSet().equals("set1") || corr2.getSet().equals("set2"));
221 }
222
223 Activity eleventh = (Activity) msgEventIter.next();
224 assertTrue("The second activity in Pick is not a Reply as expected", eleventh instanceof Reply);
225 assertEquals("The second activity in Pick name is wrong", "act11", eleventh.getName());
226 MessageEvent msgEvent2 = ((Pick)aPick).getMessageEvent(eleventh);
227 assertEquals("The message event associated with the second activity in Pick has not right link", "link2", msgEvent2.getPartnerLink());
228 {
229 Iterator corrIter2 = msgEvent2.getCorrelations().iterator();
230 CorrelationRef corr3 = (CorrelationRef) corrIter2.next();
231 assertEquals("The first correlation associated with the third activity in Pick has not right set", "set3", corr3.getSet());
232 }
233
234 Iterator alarmEventIter = ((Pick)aPick).getActivityAlarmEvents().keySet().iterator();
235 Activity twelveth = (Activity) alarmEventIter.next();
236 assertTrue("The third activity in Pick is not a Terminate as expected", twelveth instanceof Terminate);
237 assertEquals("The third activity in Pick name is wrong", "act12", twelveth.getName());
238 AlarmEvent alrmEvent = ((Pick)aPick).getAlarmEvent(twelveth);
239 assertEquals("The message event associated with the third activity in Pick has not right time expr", "2j", alrmEvent.getTimeExpression());
240 assertEquals("The message event associated with the third activity in Pick has not right type", AlarmEvent.DURATION_EXPR, alrmEvent.getType());
241
242 }
243 }
244 }
This page was automatically generated by Maven